using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Business.Entities.Config;

namespace OR.Lazarus.Business.Entities.Config.Lazarus
{
	/// <summary>
	/// Defines a Tier (Data, Business, etc.) for generation.
	/// </summary>
   public class TierDetail : EntityBase
   {
		protected System.String mName;
		protected string mType;
		protected string mFolder;
		protected string mDescription;
	
		protected Derived.TransformCollection mTransforms;
		protected Derived.IncludeCollection mIncludes;

		public TierDetail()
		{
			mTransforms = new Derived.TransformCollection();
			mTransforms.ParentEntity = this;
			mIncludes = new Derived.IncludeCollection();
			mIncludes.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Type
		{
			get
			{
				return ( mType );
			}
			set
			{
				mType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Folder
		{
			get
			{
				return ( mFolder );
			}
			set
			{
				mFolder = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.TransformCollection Transforms
		{
			get
			{
				return ( mTransforms );
			}
			set
			{
				mTransforms = value;
			}
		}

		public Derived.IncludeCollection Includes
		{
			get
			{
				return ( mIncludes );
			}
			set
			{
				mIncludes = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mType = DataUtility.GetString( pSource, "type" );
			mFolder = DataUtility.GetString( pSource, "folder" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lcfg:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vTransforms = ( XmlElement )pSource.SelectSingleNode( "lcfg:transforms", PersistenceConfig.Namespaces );
			
			if ( vTransforms != null )
			{
				mTransforms.LoadXml( vTransforms );
			}
			XmlElement vIncludes = ( XmlElement )pSource.SelectSingleNode( "lcfg:includes", PersistenceConfig.Namespaces );
			
			if ( vIncludes != null )
			{
				mIncludes.LoadXml( vIncludes );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lcfg", "tier", "http://schemas.icitadel.com/Lazarus/Config.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mType ) )
			{
				vNewElement.SetAttribute( "type", mType );
			}
					
			if ( !String.IsNullOrEmpty( mFolder ) )
			{
				vNewElement.SetAttribute( "folder", mFolder );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lcfg", "description", "http://schemas.icitadel.com/Lazarus/Config.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mTransforms.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mIncludes.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.TierDetail v_oReturn = new Derived.TierDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Type = mType;
			v_oReturn.Folder = mFolder;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.TransformDetail vDetail in mTransforms )
			{
				v_oReturn.Transforms.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.IncludeDetail vDetail in mIncludes )
			{
				v_oReturn.Includes.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.TierDetail CloneTyped()
		{
			return ( ( Derived.TierDetail )this.Clone() );
		}
   }
}
   